import { IPageQuery, IPageFuzzyQuery, IPageQueryOrders } from '../interfaces';
import { FlagEnum, SortEnum, OperatorEnum } from '../enums';

export class PageQuery<T> implements IPageQuery<T> {
  currentPage: number = 1;
  pageSize: number = 10;
  t: T | any = {};
  all: boolean;
  orders: IPageQueryOrders[];
  flag?: FlagEnum | string;
  query?: IPageFuzzyQuery[];

  /**
   * 设置查询所有
   */
  setAll(val: boolean = true) {
    this.all = val;
  }

  /**
   * 添加排序
   * @param key 排序字段
   * @param sort 默认降序
   */
  addOrder(key: string, sort: SortEnum | string = SortEnum.DESC) {
    if (!!this.orders) { this.orders = []; }
    this.orders.push({ key: key, sort: sort });
  }

  /**
   * 添加查询
   * IN,NOT_IN 多个查询，用逗号分隔
   * 
   * @param key 查询字段
   * @param value 查询内容
   * @param operator 默认相等
   */
  addQuery(key: string, value: string, operator: OperatorEnum | string = OperatorEnum.EQUALS) {
    if (!!this.query) { this.query = []; }
    this.query.push({ key: key, value: value, operator: operator });
  }
}